{include file="public/header296" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="banner">Banner管理</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="insert">添加分类</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        <a class="layui-btn layui-btn-xs layui-btn-normal " lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
<script>
  layui.use(['layer','table','laydate','form', 'element'], function(){
    var $ = layui.$
    table = layui.table
            ,laydate  = layui.laydate
            , form = layui.form;

    table.render({
      elem: '#LAY-app-content-list'
      ,url: "{:url('bannerCategory/getBannerCategoryList')}"
      , request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      , page: {
        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
        ,first: '首页'
        ,last: '尾页'
        ,limit: 10
      }
      ,cols: [[
        {field: 'id', width: 80, title: 'ID', align:'center'}
        , {field: 'name', title:'分类名称', align:'center'}
        , {field: 'status', title: "状态", align:'center', templet: function (d) {
            var is_checked = '';
            if (d.status == 1) {
              is_checked = 'checked';
            }
            return '<input type="checkbox" name="status" switch_page_id="'+ d.id +'" value="'+ d.status +'" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" '+ is_checked +'>';
          }}
        ,{field: 'update_time', title: '最后修改', align:'center'}
        ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
      ]]
      ,text: {
        none: '暂无相关数据'
      }
    });
    // 监听工具条
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var id = data.id;
      if(obj.event === 'edit'){
        layer.open({
          type: 2
          ,title: '编辑分类'
          ,content: "{:url('bannerCategory/edit')}?id=" + id
          , maxmin: true
          , area: ['50%', '60%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");

            iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
              var field = data.field;
              $.ajax({
                url:"{:url('bannerCategory/edit')}",
                type:'post',
                data:{'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    table.reload('LAY-app-content-list');
                    layer.close(index);
                  }
                  else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      } else if(obj.event === 'delete'){
        layer.confirm('确定删除该该条记录吗，请谨慎操作？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('bannerCategory/delete')}",
            type:'post',
            data:{'id':id},
            success: function(res){
              if(res.code == 0){
                obj.del();
                layer.msg(res.msg,{icon:1});
              } else {
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      }
    });

    var $ = layui.$, active = {
      insert: function(){
        layer.open({
          type: 2
          ,title: '添加分类'
          ,content: "{:url('bannerCategory/insert')}"
          ,maxmin: true
          ,area: ['50%', '60%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-submit");
            iframeWindow.layui.form.on('submit(Lay-app-form-submit)', function(data){
              var field = data.field;

              if(field.name == ''){
                return layer.msg('请选择分类', {icon:5})
              }
              $.ajax({
                url: "{:url('bannerCategory/insert')}",
                type: 'post',
                data: {'field': field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index);
                    table.reload('LAY-app-content-list');
                  } else{
                    layer.msg(res.msg,{icon:5});
                    return false;
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      },
      banner: function(){
        window.location.href = "{:url('banner/index')}";
      }
    };

    // 监听 data-type 事件
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
{include file="public/footer" /}